<?php

namespace App\Http\Controllers\Seller;


use App\Library\Mall\Shop\CatlogActions;
use App\Models\Item;
use App\Models\ItemCatlogProps;
use App\Models\ItemProps;
use App\Traits\Mall\ItemManagerTrait;
use App\WeChat\WechatDefaultConfig;
use Illuminate\Http\Request;

class ItemController extends BaseController
{

    use ItemManagerTrait, CatlogActions, WechatDefaultConfig;

    /**
     * ItemController constructor.
     * @param Request $request
     */
    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->assign(['sideMenu' => 'item']);
    }

    /**
     * @return bool
     */
    protected function authenticate()
    {
        return true;
    }

    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @throws \Exception
     */
    public function index(Request $request)
    {
        return $this->showItems($request);
    }

    /**
     * @param Request $request
     * @param \Illuminate\Pagination\Paginator $items
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    protected function showItemsView(Request $request, $items)
    {
        $this->assign(array_merge([
            'q' => '',
            'tab' => 'all'
        ], $request->all()));
        return $this->view('seller.item.index', [
            'items' => $items,
            'pagination' => $items->appends($request->except('page'))->render(),
            'shopCatlogs' => $this->fetchShopCtlogs($this->shop)
        ]);
    }

    /**
     * @param Request $request
     * @param $item
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    protected function showPublishView(Request $request, $item)
    {
        //运费模板
        $catlogOptions = $this->catlogRepository()->fetchOptions();
        $freightTemplates = $this->shop->freightTemplates()->orderBy('template_id')->get(['template_id', 'template_name']);
        return $this->view('seller.item.publish', compact('item', 'catlogOptions', 'freightTemplates'));
    }

    /**
     * @param Request $request
     * @param Item $item
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function sendSavedResponse(Request $request, Item $item)
    {
        return $this->messager()->setMessage(trans('item.item publish success'))
            ->setLinks([
                [trans('common.reedit'), url('seller/item/publish?itemid=' . $item->itemid)],
                [trans('item.publish item'), url('/seller/item/publish')],
                [trans('sysmessage.back_list'), url('/seller/item')]
            ])->render();
    }

    /**
     * @param Request $request
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
     */
    public function appCode(Request $request)
    {
        $itemid = $request->input('itemid', 0);
        $qrcode = $this->miniProgram()->app_code->getUnlimit("itemid=$itemid", [
            'page' => 'pages/item/detail',
            'width' => '400'
        ]);
        return $qrcode;
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function getProps()
    {
        $catid = $this->request->input('catid');
        $properties = ItemCatlogProps::where('catid', $catid)->orderBy('prop_id')->get();

        $itemid = $this->request->input('itemid');
        $itemProps = [];
        ItemProps::where('itemid', $itemid)->get()->map(function ($prop) use (&$itemProps) {
            $itemProps[$prop->prop_id] = $prop;
        });

        $table = '<table class="props-table" cellpadding="0" cellspacing="0"><tr>';
        foreach ($properties as $key => $prop) {
            $default = $prop->default;
            if (isset($itemProps[$prop->prop_id])) {
                $default = ($itemProps[$prop->prop_id])->prop_value;
            }
            $name = 'properties[' . $prop->prop_id . ']';
            if ($prop->type == 'text') {
                $table .= '<td class="td-label">' . $prop->title . '</td>';
                $table .= '<td class="td-input"><input type="text" class="form-control w200" name="' . $name . '" value="' . $default . '"></td>';
            }

            if ($prop->type == 'textarea') {
                $table .= '<td class="td-label">' . $prop->title . '</td>';
                $table .= '<td class="td-input"><textarea class="form-control" name="' . $name . '" style="width: 200px; height: 60px">' . $default . '</textarea></td>';
            }

            if ($prop->type == 'number') {
                $table .= '<td class="td-label">' . $prop->title . '</td>';
                $table .= '<td class="td-input"><input type="number" class="form-control w200" name="' . $name . '" value="' . $default . '"></td>';
            }

            if ($prop->type == 'calendar') {
                $table .= '<td class="td-label">' . $prop->title . '</td>';
                $table .= '<td class="td-input"><input type="text" class="form-control w200" readonly="readonly" onclick="WdatePicker()" name="' . $name . '" value="' . $default . '"></td>';
            }

            if ($prop->type == 'select') {
                $table .= '<td class="td-label">' . $prop->title . '</td>';
                $table .= '<td class="td-input"><select class="form-control w200" name="' . $name . '">';

                $options = explode("\n", $prop->options);
                foreach ($options as $k => $v) {
                    $selected = $v == $default ? ' selected="selected"' : '';
                    $table .= '<option value="' . $v . '"' . $selected . '>' . $v . '</option>';
                }
                $table .= '</select></td>';
            }

            if ($prop->type == 'radio') {
                $table .= '<td class="td-label">' . $prop->title . '</td>';
                $table .= '<td class="td-input">';

                $options = explode("\n", $prop->options);
                foreach ($options as $k => $v) {
                    $checked = $v == $default ? ' checked="checked"' : '';
                    $table .= '<label><input type="radio" name="' . $name . '" value="' . $v . '"' . $checked . '>' . $v . '</label>';
                }
                $table .= '</td>';
            }

            if ($prop->type == 'checkbox') {
                $table .= '<td class="td-label">' . $prop->title . '</td>';
                $table .= '<td class="td-input">';

                $options = explode("\n", $prop->options);
                foreach ($options as $k => $v) {
                    $checked = $v == $default ? ' checked="checked"' : '';
                    $table .= '<label><input type="checkbox" name="' . $name . '[]" value="' . $v . '"' . $checked . '>' . $v . '</label>';
                }
                $table .= '</td>';
            }

            if (($key + 1) % 2 == 0) {
                $table .= '</tr><tr>';
            }
        }
        $table .= '</tr></table>';
        return ajaxReturn(['table' => $table]);
    }
}
